package com.kaishengit.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.entity.Progress;
import com.kaishengit.entity.Task;
import com.kaishengit.service.ProgressService;
import com.kaishengit.service.TaskService;

public class NewTaskAdd extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		
		String taskname = request.getParameter("taskname");
		String taskdesc = request.getParameter("taskdesc");
		String taskstate = request.getParameter("taskstate");
		String tasklevel = request.getParameter("tasktype");
		String taskmanager = request.getParameter("taskmanager");
		String taskproject = request.getParameter("taskproject");
		//System.out.println("222222222222222222222222"+taskproject);
		String taskgoal = request.getParameter("taskgoal");
		
		TaskService taskService = new TaskService();
		
		
		
		List<Task> taskOlds = taskService.findTasknamesByGoalname(taskgoal,taskproject);
		
		boolean flag = true;
		
		for(Task task1 :  taskOlds) {
			if(task1.getName().equals(taskname)) {
				flag = false;
				response.sendRedirect("newtaskview.jspx?state=10001");
			}
		}
		
		if(flag) {
			Task task = new Task();
			UUID uuid = UUID.randomUUID();
			task.setId(uuid.toString());
			task.setName(taskname);
			task.setDesc(taskdesc);
			task.setState(taskstate);
			task.setLevel(tasklevel);
			task.setGoalid(taskgoal);
			task.setProjectid(taskproject);
			task.setEmployeeid(taskmanager);
			task.setBegintime(DateUtil.getNow());
			task.setEndtime(DateUtil.getNow());
			task.setCreatetime(DateUtil.getNow());
			
			taskService.save(task);
			
			
			Progress progress = new Progress();
			progress.setProjectid(taskproject);
			progress.setGoalid(taskgoal);
			progress.setTaskid(taskname);
			progress.setProgressValue(0);
			
			ProgressService progressService = new ProgressService();
			progressService.updateTask(progress);
			response.sendRedirect("goallistview.jspx?project="+taskproject);
		}
	}

}
